package com.ruoyi.web.service.warning.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.constant.StateConstants;
import com.ruoyi.web.domain.warning.WarningInsect;
import com.ruoyi.web.enums.DeviceTypeEnum;
import com.ruoyi.web.enums.InsectTypeEnum;
import com.ruoyi.web.mapper.warning.WarningInsectMapper;
import com.ruoyi.web.pojo.request.insect.InsectListRequest;
import com.ruoyi.web.pojo.request.warning.WarningInsectRequest;
import com.ruoyi.web.service.device.DeviceInfoService;
import com.ruoyi.web.service.insect.InsectRecordService;
import com.ruoyi.web.service.warning.WarningInsectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 预警昆虫信息表 服务实现类
 * @author fwx
 * @date 2024-02-01 14:44:37
 */
@Service
public class WarningInsectServiceImpl extends ServiceImpl<WarningInsectMapper, WarningInsect> implements WarningInsectService {

    @Autowired
    private InsectRecordService insectRecordService;

    @Autowired
    private DeviceInfoService deviceInfoService;

    /**
    * 新增预警昆虫信息表
    */
    @Override
    public void insert(WarningInsect WarningInsect){
        super.save(WarningInsect);
    }

    /**
    * 修改预警昆虫信息表
    */
    @Override
    public void update(WarningInsect WarningInsect){
        super.updateById(WarningInsect);
    }

    @Override
    public void updateEnable(Integer id, Integer flag){
        //处理错误的值
        if (!Objects.equals(flag,StateConstants.ENABLE)){
            flag = StateConstants.DEACTIVATE;
        }

        UpdateWrapper<WarningInsect> wrapper = new UpdateWrapper<>();
        wrapper.lambda().set(WarningInsect::getIsEnable,flag)
                .eq(WarningInsect::getId,id)
        ;

        super.update(wrapper);
    }

    @Override
    public void saveOrUpdateInfo(WarningInsect insect){
        if(insect.getId() == null){
            throw new RuntimeException("请传入id");
        }

        this.update(insect);
    }

    /**
    * 删除预警昆虫信息表
    */
    @Override
    public void delete(String id){
        super.removeById(id);
    }

    /**
    *  分页查询预警昆虫信息表
    */
    @Override
    public void queryPage(IPage<WarningInsect> page){
        QueryWrapper<WarningInsect> query  = new QueryWrapper<>();
        super.page(page,query);
    }

    @Override
    public List<WarningInsect> queryPage(WarningInsectRequest request){
        QueryWrapper<WarningInsect> query  = new QueryWrapper<>();
        request.bulidWrapper(query);
        List<WarningInsect> list = super.list(query);
        return list;
    }

    @Override
    public List<WarningInsect> getInsectList(InsectListRequest request){
        QueryWrapper<WarningInsect> query  = new QueryWrapper<>();
        request.bulidWrapper(query);

        List<WarningInsect> list = super.list(query);

        List<String> deviceIds = deviceInfoService.getIdsByUser(DeviceTypeEnum.FL2.getValue());

        Set<String> insctSet = insectRecordService.getInsctName(request.getStartTime(), request.getEndTime(),deviceIds);

        list = list.stream().filter(ins -> insctSet.contains(ins.getInsectName()) || Objects.equals(InsectTypeEnum.ONE.getValue(),ins.getInsectType()))
                .collect(Collectors.toList());

        return list;
    }

    /**
    * 查询所有预警昆虫信息表
    */
    @Override
    public List<WarningInsect> queryAll(){
        QueryWrapper<WarningInsect> query  = new QueryWrapper<>();
        List<WarningInsect> list = super.list(query);
        return list;
    }

    @Override
    public Map<String,WarningInsect> getAllMap(){
        List<WarningInsect> warningInsects = this.queryAll();

        Map<String, WarningInsect> result = warningInsects.stream()
                .collect(Collectors.toMap(WarningInsect::getInsectName, Function.identity(), (x, y) -> x));

        return result;
    }

    @Override
    public List<WarningInsect> getEnabled(){
        QueryWrapper<WarningInsect> query  = new QueryWrapper<>();
        query.lambda().eq(WarningInsect::getIsEnable,StateConstants.ENABLE);
        List<WarningInsect> list = super.list(query);
        return list;
    }


    /**
    * 根据id查询预警昆虫信息表
    */
    @Override
    public WarningInsect findById(String id){
        WarningInsect res = super.getById(id);
        return res;
    }

}
